<?

include_once("includes/inc.global.php");

$p->site_section = EXCHANGES;
$p->page_title = "Record an Exchange";

include("classes/class.trade.php");
include("includes/inc.forms.validation.php");

//
// Define form elements
//
$member = new cMember;

if($cUser->member_id == "ADMIN") {
	$p->DisplayPage("I'm sorry, you cannot record exchanges while logged in as the ADMIN account.  This is a special account for administration purposes only.<p>To create member accounts go to the <a href=admin_menu.php>Administration menu</a>.");	
	exit;
}

if($_REQUEST["mode"] == "admin") {
	$cUser->MustBeLevel(2);
	$member->LoadMember($_REQUEST["member_id"]);
	$p->page_title .= " for ". $member->PrimaryName();
} else {
	$cUser->MustBeLoggedOn();
	$member = $cUser;
}	
	
$form->addElement('hidden', 'member_id', $member->member_id);
$form->addElement('hidden', 'mode', $_REQUEST["mode"]);
$form->addElement("html", "<TR></TR>");  // TODO: Move this to the header
$name_list = new cMemberGroup;
$name_list->LoadMemberGroup();
$form->addElement("select", "member_to", "Transfer to Member", $name_list->MakeNameArray());
$category_list = new cCategoryList();
$form->addElement('select', 'category', 'Category', $category_list->MakeCategoryArray());
$form->addElement("text", "units", "# of ". UNITS ." to Transfer", array('size' => 5, 'maxlength' => 10));
if(UNITS == "Hours") {
	$form->addElement("text","minutes","# of Minutes to Transfer",array('size'=>2,'maxlength'=>2));
}
$form->addElement('static', null, 'Enter a Brief Description of the Exchange', null);
$form->addElement('textarea', 'description', null, array('cols'=>50, 'rows'=>4, 'wrap'=>'soft'));
$form->addElement('submit', 'btnSubmit', 'Submit');

//
// Define form rules
//
//$form->addRule('description', 'Enter a description', 'required');
$form->registerRule('verify_not_self','function','verify_not_self');
$form->addRule('member_to', 'You cannot transfer to yourself', 'verify_not_self');
$form->registerRule('verify_selection','function','verify_selection');
$form->addRule('category', 'Choose a category', 'verify_selection');
$form->addRule('member_to', 'Choose a member', 'verify_selection');
$form->addRule('description', 'Description too long - maximum length is 255 characters', 'verify_max255');

if(UNITS == "Hours") {
	$form->registerRule('verify_whole_hours','function','verify_whole_hours');
	$form->addRule('units', 'Hours entered must be a positive, whole number', 'verify_whole_hours');
	$form->registerRule('verify_even_minutes','function','verify_even_minutes');
	$form->addRule('minutes', 'Enter 15, 30, or 45 (or other numbers in 3 minute increments)', 'verify_even_minutes');
} else {
	$form->registerRule('verify_valid_units','function','verify_valid_units');
	$form->addRule('units', 'Enter a positive number with no more than two decimal points', 'verify_valid_units');
}


//
// Then check if we are processing a submission or just displaying the form
//
if ($form->validate()) { // Form is validated so processes the data
   $form->freeze();
 	$form->process('process_data', false);
} else {
   $p->DisplayPage($form->toHtml());  // just display the form
}

function process_data ($values) {
	global $p, $member, $cErr;
	$list = "";
	
	if(UNITS == "Hours") {
		if($values['minutes'] > 0)
			$values['units'] = $values['units'] + ($values['minutes'] / 60);
	}
	
	if(!($values['units'] > 0)) {
		$cErr->Error("No units were entered to exchange!");
		include("redirect.php");
	}
	
	$member_to_id = substr($values['member_to'],0, strpos($values['member_to'],"?")); // TODO:
	$member_to = new cMember;
	$member_to->LoadMember($member_to_id);
	
	if ($_REQUEST["mode"] == "admin")
		$type = TRADE_BY_ADMIN; // record that trade was entered by an admin & log if logging enabled
	else 
		$type = TRADE_ENTRY;  // regular trade
	
	$trade = new cTrade($member, $member_to, $values['units'], $values['category'], $values['description'], $type);
	$status = $trade->MakeTrade();
	if(!$status)
		$list .= "Trade failed!  Please try again later.";
	else
		$list .= "You have transferred ". $values['units'] ." ". strtolower(UNITS) ." to ". $member_to_id .".  Would you like to <A HREF=trade.php?mode=".$_REQUEST["mode"]."&member_id=". $_REQUEST["member_id"].">record another</A> exchange?<P>Or would you like to leave <A HREF=feedback.php?mode=". $_REQUEST["mode"] ."&author=". $member->member_id ."&about=". $member_to_id ."&trade_id=". $trade->trade_id .">feedback</A> for this member?";
		
   $p->DisplayPage($list);
}

function verify_not_self($element_name,$element_value) {
	global $member;
	$member_id = substr($element_value,0, strpos($element_value,"?"));
	if ($member_id == $member->member_id)
		return false;
	else
		return true;
}

function verify_valid_units($element_name,$element_value) { 
	if ($element_value < 0)
		return false; 
	elseif ($element_value * 100 != floor($element_value * 100)) 
		return false;	// allow no more than two decimal points
	else
		return true;
}

function verify_even_minutes ($z, $minutes) { // verifies # of minutes entered represents an evenly
	if($minutes/60*1000 == floor($minutes/60*1000)) 	// divisible fraction w/ no more than 3
		return true;												//	decimal points
	else
		return false;
}

function verify_whole_hours ($z, $hours) {
	if(abs(floor($hours)) != $hours)
		return false;
	else
		return true;
}

?>
